package org.example.service.impl;

import cn.hutool.json.JSONUtil;
import org.example.mapper.TransferRecordMapper;
import org.example.model.TransferRecord;
import org.example.service.TransferRecordService;
import org.example.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author lijun
 * @date 2025-10-20 13:14
 */
@Service
public class TransferRecordServiceImpl implements TransferRecordService {

    @Autowired
    private TransferRecordMapper transferRecordMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public List<TransferRecord> getAllRecords() {
        return transferRecordMapper.selectAll();
    }

    @Override
    public TransferRecord queryCache(Integer id) {
        Object dataStr = redisUtil.get("record:" + id);
        if(null != dataStr){
            return JSONUtil.toBean(dataStr+"", TransferRecord.class);
        }
        TransferRecord  transferRecord = transferRecordMapper.selectById(id);
        if (transferRecord != null) {
            redisUtil.set("record:" + id, JSONUtil.toJsonStr(transferRecord));
        }
        return transferRecord;
    }
    @Override
    public TransferRecord getRecordById(Integer id) {
        return transferRecordMapper.selectById(id);
    }
    @Override
    public boolean addRecord(TransferRecord record) {
        return transferRecordMapper.insert(record) > 0;
    }

    @Override
    public boolean updateRecord(TransferRecord record) {
        return transferRecordMapper.update(record) > 0;
    }

    @Override
    public boolean deleteRecord(Integer id) {
        return transferRecordMapper.deleteById(id) > 0;
    }

    @Override
    public List<TransferRecord> searchRecords(String channel, String type, Date startTime, Date endTime) {
        return transferRecordMapper.selectByCondition(channel, type, startTime, endTime);
    }
}
